﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMMN202 : Form
    {
        private String sFlagEditMode = "NONE";
        private String sActivePopupType = String.Empty;
        private String sActivePopupItem = String.Empty;

        public FMMN202()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void wf_popup_list_Load(object sender, EventArgs e)
        {
            GetPopopType();
        }

        private void GetPopopType()
        {

            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;
            //
            cb_POPUP_TYPE.Items.Clear();
            strQuery = String.Empty;
            strQuery += " SELECT [POPUP_TYPE]";
            strQuery += " FROM [POPUP_TYPE_MASTER] ";
            strQuery += " ORDER BY [POPUP_TYPE]";
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            while (sqlRead.Read())
            {
                cb_POPUP_TYPE.Items.Add(sqlRead[0].ToString());

            }
            sqlRead.Close();
            sqlConn.Close();
        }

        private void GetPopopItem(String pPopupType)
        {
            
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;
            //
            lb_POPUP_ITEM.Items.Clear();
            strQuery = String.Empty;
            strQuery += " SELECT [POPUP_TEXT]";
            strQuery += " FROM [POPUP_MASTER] ";
            strQuery += " WHERE [POPUP_TYPE] = '" + pPopupType.ToString() + "'";
            strQuery += " ORDER BY [POPUP_TEXT]";
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            while (sqlRead.Read())
            {
                lb_POPUP_ITEM.Items.Add(sqlRead[0].ToString());
                 
            }
            sqlRead.Close();
            sqlConn.Close();
        }

          private void SavePopupItem()
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            switch (sFlagEditMode)
            {
                case "ADD":
                    strQuery = String.Empty;
                    strQuery += "INSERT INTO [POPUP_MASTER] VALUES (";
                    strQuery += "'" + sActivePopupType.ToString() + "',";
                    strQuery += "'" + sActivePopupItem.ToString() + "')";
                break;
                case "EDIT":
                    strQuery = String.Empty;
                    strQuery += "UPDATE [POPUP_MASTER] ";
                    strQuery += " SET [POPUP_TEXT] = '" + txt_POPUP_ITEM.Text + "'";
                    strQuery += " WHERE [POPUP_TYPE] = '" + sActivePopupType.ToString() + "'";
                    strQuery += " AND [POPUP_TEXT] = '" + sActivePopupItem.ToString() + "'";
                break;
                case "DELETE":
                    strQuery = String.Empty;
                    strQuery += "DELETE FROM [POPUP_MASTER] ";
                    strQuery += " WHERE [POPUP_TYPE] = '" + sActivePopupType.ToString() + "'";
                    strQuery += " AND [POPUP_TEXT] = '" + sActivePopupItem.ToString() + "'";
                break;
            }
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlComm.ExecuteNonQuery();
            sqlConn.Close();
        }

        private void but_ADD_Click(object sender, EventArgs e)
        {
            if (cb_POPUP_TYPE.SelectedItem != null)
            {
                sFlagEditMode = "ADD";
                sActivePopupType = cb_POPUP_TYPE.SelectedItem.ToString();
                sActivePopupItem = String.Empty;
                EnableEdit(true);
            }
            else
            {
                sFlagEditMode = "NONE";
                sActivePopupType = String.Empty;
                sActivePopupItem = String.Empty;
            }
        }

        private void but_CLOSE_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EnableEdit(bool pEnabled)
        {
            cb_POPUP_TYPE.Enabled = !pEnabled;
            lb_POPUP_ITEM.Enabled = !pEnabled;
            but_ADD.Enabled = !pEnabled;
            but_EDIT.Enabled = !pEnabled;
            but_DELETE.Enabled = !pEnabled;
            but_CLOSE.Enabled = !pEnabled;
            but_SAVE.Enabled = pEnabled;
            but_CANCEL.Enabled = pEnabled;
            txt_POPUP_ITEM.Enabled = pEnabled;
            txt_POPUP_ITEM.Focus();
        }


        private void but_DELETE_Click(object sender, EventArgs e)
        {
            if (lb_POPUP_ITEM.SelectedItem!= null)
            {
                if (PCMSG.Show("Do you want to delete popup item " + lb_POPUP_ITEM.SelectedItem.ToString() + "?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    PCMSG.Show("Popup item deleted successfully.");
                    GetPopopItem("");
                }
            }
        }

        private void lb_BATCH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lb_POPUP_ITEM.SelectedItem != null)
            {
                ClrFld();
            }
        }

        private void butRefresh_Click(object sender, EventArgs e)
        {
            //if (lb_POPUP_ITEM.SelectedItem != null)
            //{
            //    ClrFld();
            //    GetBatchSummary(lb_POPUP_ITEM.SelectedItem.ToString());
            //}
        }

        private void ClrFld()
        {
            //txt_POPUP_ITEM.Text = String.Empty;
            //if (lb_POPUP_ITEM.SelectedItem != null)
            //{
            //     txt_POPUP_ITEM.Text = lb_POPUP_ITEM.SelectedItem.ToString();
            //}
        }

        private void but_UNLOCK_Click(object sender, EventArgs e)
        {
            if (lb_POPUP_ITEM.SelectedItem != null)
            {
                if (PCMSG.Show("Do you want to unlock forms in the batch " + lb_POPUP_ITEM.SelectedItem.ToString() + "?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    //UnlockBatch(lb_POPUP_ITEM.SelectedItem.ToString());
                    PCMSG.Show("Batch unlocked successfully.");
                    ClrFld();
                    //GetBatchSummary(lb_POPUP_ITEM.SelectedItem.ToString());
                }
            }
        }

        private void but_EXPORT_Click(object sender, EventArgs e)
        {
            if (lb_POPUP_ITEM.SelectedItem != null)
            {
                if (PCMSG.Show("Do you want to export batch " + lb_POPUP_ITEM.SelectedItem.ToString() + "?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    //ExportBatch(lb_POPUP_ITEM.SelectedItem.ToString());
                    PCMSG.Show("Batch exported successfully.");
                    ClrFld();
                    //GetBatchSummary(lb_POPUP_ITEM.SelectedItem.ToString());
                }
            }
        }

        private void but_UNSKIP_Click(object sender, EventArgs e)
        {
            if (lb_POPUP_ITEM.SelectedItem != null)
            {
                if (PCMSG.Show("Do you want to unskip forms in the batch " + lb_POPUP_ITEM.SelectedItem.ToString() + "?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    //UnskipBatch(lb_POPUP_ITEM.SelectedItem.ToString());
                    PCMSG.Show("Batch unskipped successfully.");
                    ClrFld();
                    //GetBatchSummary(lb_POPUP_ITEM.SelectedItem.ToString());
                }
            }
        }

        private void cb_POPUP_TYPE_SelectedIndexChanged(object sender, EventArgs e)
        {
            lb_POPUP_ITEM.Items.Clear(); 
        }

        private void but_REFRESH_Click(object sender, EventArgs e)
        {
            if (cb_POPUP_TYPE.SelectedItem != null)
            {
                GetPopopItem(cb_POPUP_TYPE.SelectedItem.ToString()); 
            }
        }

        private void but_EDIT_Click(object sender, EventArgs e)
        {
            sFlagEditMode = "EDIT";
            EnableEdit(true);
        }

        private bool FldVal()
        {
            if (cb_POPUP_TYPE.SelectedItem == null)
            {
                PCMSG.Show("Popup type is not selected or empty");
                return false;
            }
            //else if (lb_POPUP_ITEM.SelectedItem == null && sFlagEditMode != "ADD")
            //{
            //    PCMSG.Show("Popup item is not selected or empty");
            //    return false;
            //}
            else if (txt_POPUP_ITEM.Text.Length == 0)
            {
                PCMSG.Show("Popup item should not be empty");
                return false;
            }
            return true;
        }

        private void but_SAVE_Click(object sender, EventArgs e)
        {
            if (FldVal())
            {
                sActivePopupType = cb_POPUP_TYPE.SelectedItem.ToString();
                SavePopupItem();
                switch (sFlagEditMode)
                {
                    case "ADD":
                        sActivePopupType = txt_POPUP_ITEM.Text;
                        PCMSG.Show("Popup item added successfully.");
                        break;
                    case "EDIT":
                        sActivePopupType = lb_POPUP_ITEM.SelectedItem.ToString();
                        SavePopupItem();
                        PCMSG.Show("Popup item modified successfully.");
                        break;
                    case "DELETE":
                        sActivePopupType = lb_POPUP_ITEM.SelectedItem.ToString();
                        SavePopupItem();
                        PCMSG.Show("Popup item deleted successfully.");  
                        break;
                }
                EnableEdit(false);
            }
        }

        private void but_CANCEL_Click(object sender, EventArgs e)
        {
            EnableEdit(false);
        }


    }
}
